<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Qcloud\Sms\SmsSingleSender;

class SmsController extends AdminController
{
    function SendMsg(Request $request){
        // 参数1000-9999的随机数
        $code = mt_rand(1000,9999);
        $code = str_pad($code,6,0,STR_PAD_RIGHT);
        $phone = $request->get('phone');

        $this->Send($phone,1807300,['您好']);
    }
    public function Send($phone,$templateId,$params){
        // 发送手机短信
        try{
            $this->sms = config('sms');
            $sender = new SmsSingleSender($this->sms['app_id'],$this->sms['app_key']);
//            $templateId = '1819875';
            $result = $sender->sendWithParam(
                '86',
                $phone,
                $templateId,
                $params,
                $this->sms['smsSign'],
                "",
                ""
            );
            //返回发送结果
            $rsp = json_decode($result,true);
            if($rsp['result']==0){
                return "{
                  \"status\":0,
                  \"result\":\"logo\"
                }";
            }else{
                dd($rsp);
            }

        }catch (Exception $e){
            return self::error(4025,$e);
        }
    }
    public function SendCode(Request $request){
        $validator = Validator::make($request->all(), [
            'phone' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make($request->all(), [
            'phone' => 'regex:/^1[345789][0-9]{9}$/',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_PHONE_UNREGISTERED);
        }
        $code = mt_rand(1000,9999);
        $code = str_pad($code,6,0,STR_PAD_RIGHT);
        $phone = $request->phone;
        $this->Send($phone,1807300,[$code]);
        $key = time().rand(100,999);
        $minute = 90;//验证码的有效时间，单位为秒
        Cache::put($phone.$key,$code,$minute);//放入缓存
        $rsp['key'] = $key;
        $rsp['code'] = $code;
        $rsp['msg'] = $phone.$key;
        $rsp['minute'] = $minute;
        return $this->success($rsp);
    }
    public function verifyCodeCorrect(Request $request){
        $validator = Validator::make($request->all(), [
            'phone' => 'required|string',
            'key' => 'required|string',
            'code' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make($request->all(), [
            'phone' => 'regex:/^1[345789][0-9]{9}$/',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_PHONE_UNREGISTERED);
        }
        $key = $request->phone.$request->key;
        //判断是否还存在
        if(!Cache::has($key)) {
            return $this->fail(CodeResponse::AUTH_CAPTCHA_EXPIRED);
        }
        $cache_code = Cache::get($key);//获取
        if($request->code != $cache_code){
            return $this->fail(CodeResponse::AUTH_CAPTCHA_UNMATCH);
        }
        Cache::forget($key);//释放
        return $this->success();
    }
}
